# class Solution:
#     def decodeString(self, s: str) -> str:
#         ret=""
#         i=0
#         while i<len(s):
#             if s[i]>"0" and s[i]<"9":
#                 count=int(s[i])
#                 i+=1
#                 while s[i]>="0" and s[i]<"9":
#                     count=count*10+int(s[i])
#                     i+=1
#                 i+=1
#                 e=""
#                 while s[i]!="]":
#                     e=e+s[i]
#                     i+=1
#                 i+=1
#                 ret+=count*e
#             else:
#                 ret+=s[i]
#                 i+=1
#         return ret

# s=Solution()
# print(s.decodeString( "abc3[cd]xyz"))


class Solution:
    def decodeString(self, s: str) -> str:
        def decode(source):
            i=0
            ret=""
            stack=0
            while i<len(source):
                if source[i]>'0' and source[i]<='9':
                    rep=0
                    while source[i]!="[":
                        rep=rep*10+int(source[i])
                        i+=1
                    stack+=1
                    i+=1
                    j=i
                    while stack:
                        if source[j]=="[":
                            stack+=1
                        elif source[j]=="]":
                            stack-=1
                        j+=1
                    ret+=rep*decode(source[i:j-1])
                    i=j      
                else:
                    ret+=source[i]
                    i+=1
            return ret
        return decode(s)

# 执行用时：
# 40 ms
# , 在所有 Python3 提交中击败了
# 55.96%
# 的用户
# 内存消耗：
# 14.7 MB
# , 在所有 Python3 提交中击败了
# 94.01%
# 的用户
                    